/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package docking;

import java.util.ArrayList;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

/**
 *
 * @author DJH
 */
public class NewReservation extends javax.swing.JPanel {

    private DataHandler dataHandler;
    private JDialog jd;
    /**
     * Creates new form NewReservation
     */
    public NewReservation(DataHandler data, JDialog jd) {
        dataHandler = data;
        this.jd = jd;
        initComponents();
        populateList();
        populateCombobox();
    }
    
    private void populateList()
    {
        
        ArrayList<Boat> temp = dataHandler.getBoats();
        
        String[] tempArray = new String[temp.size()];
        int index = temp.size();
        
        for(int i = 0; i < tempArray.length; i++)
        {
            
                tempArray[i] = temp.get(i).getName();
            
        }
        shipsList.setListData(tempArray);
    }
    
    private void populateCombobox()
    {
        comboBox.removeAll();
        ArrayList<Pier> temp = dataHandler.getPiers();
        for(Pier p : temp)
        {
            comboBox.addItem(p);
        }
        
    }
    
    private void selectBoat()
    {
        int index = shipsList.getSelectedIndex();
        Boat b = dataHandler.getBoats().get(index);
        
        lblName.setText(b.getName());
        lblContact.setText(b.getInfo());
        lblLength.setText(b.getLength() + "");
        lblDepth.setText(b.getDepth() + "");
        
        
    }
    
    private void creatReservation()
    {
        int index = shipsList.getSelectedIndex();
        Boat boat = dataHandler.getBoats().get(index);
        
        
        int fromYear = 0;
        int fromMonth = 0;
        int fromDay = 0;
        int fromHour = 0;
        
        int toYear = 0;
        int toMonth = 0;
        int toDay = 0;
        int toHour = 0;
        
        try
        {
            
            fromYear = Integer.parseInt(fromYearField.getText());
            fromMonth = Integer.parseInt(fromMonthField.getText());
            fromDay = Integer.parseInt(fromDayField.getText());
            fromHour = Integer.parseInt(fromKlokkanField.getText());
            
            
            toYear = Integer.parseInt(toYearField.getText());
            toMonth = Integer.parseInt(toMonthField.getText());
            toDay = Integer.parseInt(toDayField.getText());
            toHour = Integer.parseInt(toKlokkanField.getText());
            
            //kannar um tað er valid number
            if(fromYear < 2010|| fromYear > 2500)
            {
                throw new Exception("From Year var ikki í ordan");
            }
            if(toYear < 2010|| toYear > 2500)
            {
                throw new Exception("From Year var ikki í ordan");
            }
            if(fromYear < 1|| fromMonth > 12)
            {
                throw new Exception("From month var ikki í ordan");
            }
            if(toMonth < 1|| toMonth > 12)
            {
                throw new Exception("To month var ikki í ordan");
            }
            if(fromDay < 1|| fromDay > 31)
            {
                throw new Exception("From Day var ikki í ordan");
            }
            if(toDay < 1|| toDay > 31)
            {
                throw new Exception("To Day var ikki í ordan");
            }
            if(fromHour < 0|| fromHour > 23)
            {
                throw new Exception("From Hour var ikki í ordan");
            }
            if(toHour < 0|| toHour > 23)
            {
                throw new Exception("To Hour var ikki í ordan");
            }
            
            
            
            
            /*
             * 
            
            //skal kanna um from kanska er aftaná... tvs throw error
            serch: {
                
                if(fromYear > toYear)
                {
                    throw new Exception("To Year skal vera størri enn From Year ");
                }
                
                //Kannar um tað er í hvør sínum árið
                //um so er, break
                if(fromMonth > toMonth)
                {
                    if(fromYear < toYear)
                        break serch;
                    throw new Exception("To Month skal vera størri enn From Month ");
                    
                }
                
                if(fromMonth < toMonth)
                    if(fromYear == toYear)
                        break serch;
                   
                if(fromDay > toDay)
                {
                    if(fromMonth == toMonth)
                    {
                        if(fromYear == toYear)
                        throw new Exception("To Month skal vera størri enn From Month ");
                    }
                }
                
                if(fromHour > toHour)
                {
                    
                }
            
            }
            */
        }
        catch (Exception e) 
        {
            JOptionPane.showMessageDialog(this, "EX: " + e); 
            return;
        }
        
        Reservation temp = new Reservation(boat, fromYear, fromMonth, fromDay, fromHour, toYear, toMonth, toDay, toHour, dataHandler.getReservationNumber());
        
        Pier tp = (Pier)comboBox.getSelectedItem();
        
        dataHandler.addReservation(temp);
        tp.addReservation(temp);
        boat.addReservation(temp);
        
        close();
        
    }
    
    private void close()
    {
        jd.dispose();
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        shipsList = new javax.swing.JList();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        lblName = new javax.swing.JLabel();
        lblContact = new javax.swing.JLabel();
        lblLength = new javax.swing.JLabel();
        lblDepth = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        fromDayField = new javax.swing.JTextField();
        toDayField = new javax.swing.JTextField();
        fromYearField = new javax.swing.JTextField();
        fromMonthField = new javax.swing.JTextField();
        fromKlokkanField = new javax.swing.JTextField();
        toMonthField = new javax.swing.JTextField();
        toYearField = new javax.swing.JTextField();
        toKlokkanField = new javax.swing.JTextField();
        comboBox = new javax.swing.JComboBox();
        cancelButton = new javax.swing.JButton();
        creatButton = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();

        jLabel1.setText("New Rservation");

        jLabel2.setText("Ships");

        shipsList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        shipsList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                shipsListMousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(shipsList);

        jLabel3.setText("Name");

        jLabel4.setText("Contact");

        jLabel5.setText("Length");

        jLabel6.setText("Depth");

        lblName.setText("jLabel7");

        lblContact.setText("jLabel8");

        lblLength.setText("jLabel9");

        lblDepth.setText("jLabel10");

        jLabel11.setText("From");

        jLabel12.setText("To");

        fromDayField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fromDayFieldActionPerformed(evt);
            }
        });

        fromYearField.setText("2012");

        toYearField.setText("2012");

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        creatButton.setText("Creat Reservation");
        creatButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                creatButtonActionPerformed(evt);
            }
        });

        jLabel7.setText("Day");

        jLabel8.setText("Month");

        jLabel9.setText("Year");

        jLabel10.setText("Klokkan");

        jLabel13.setText("Pier");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jLabel2))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel11)
                            .addComponent(jLabel12))
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(toDayField, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                            .addComponent(toMonthField, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addComponent(jLabel7))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel8)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(fromDayField, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                            .addComponent(fromMonthField, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel9)
                                    .addComponent(fromYearField, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(toYearField, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(creatButton))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cancelButton)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(jLabel10)
                                    .addComponent(fromKlokkanField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 42, Short.MAX_VALUE)
                                    .addComponent(toKlokkanField, javax.swing.GroupLayout.Alignment.LEADING))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(comboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(8, 8, 8)
                                        .addComponent(jLabel13))))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6))
                        .addGap(49, 49, 49)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblDepth)
                            .addComponent(lblLength)
                            .addComponent(lblContact)
                            .addComponent(lblName))))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(74, 74, 74)
                .addComponent(jLabel1)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(lblName))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(lblContact))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(lblLength))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(lblDepth)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9)
                    .addComponent(jLabel10)
                    .addComponent(jLabel7)
                    .addComponent(jLabel13))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(fromYearField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(fromMonthField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(fromKlokkanField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(fromDayField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(toDayField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(toYearField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(toKlokkanField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(toMonthField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(creatButton))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void shipsListMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_shipsListMousePressed
        selectBoat();
    }//GEN-LAST:event_shipsListMousePressed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
       close();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void creatButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_creatButtonActionPerformed
        creatReservation();
    }//GEN-LAST:event_creatButtonActionPerformed

    private void fromDayFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fromDayFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_fromDayFieldActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JComboBox comboBox;
    private javax.swing.JButton creatButton;
    private javax.swing.JTextField fromDayField;
    private javax.swing.JTextField fromKlokkanField;
    private javax.swing.JTextField fromMonthField;
    private javax.swing.JTextField fromYearField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblContact;
    private javax.swing.JLabel lblDepth;
    private javax.swing.JLabel lblLength;
    private javax.swing.JLabel lblName;
    private javax.swing.JList shipsList;
    private javax.swing.JTextField toDayField;
    private javax.swing.JTextField toKlokkanField;
    private javax.swing.JTextField toMonthField;
    private javax.swing.JTextField toYearField;
    // End of variables declaration//GEN-END:variables
}
